#include<iostream>
using namespace std;
const int m = 3, n = 4;
int arr[m][n] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };
int sum[m][n];
void pre_sum()
{
	sum[0][0] = arr[0][0];
	for (int i = 1; i < m; i++)
	{
		sum[i][0] += arr[i][0] + sum[i - 1][0];
	}
	for (int i = 1; i < n; i++)
	{
		sum[0][i] += arr[0][i] + sum[0][i - 1];
	}
	for (int i = 1; i < m; i++)
	{
		for (int j = 1; j < n; j++)
		{
			sum[i][j] += arr[i][j] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
		}
	}
}
int get_sum(int x1, int y1, int x2, int y2)
{
	if (!x1 && !y1)
	{
		return sum[x2][y2];
	}
	if (!x1)
	{
		return sum[x2][y2] - sum[x2][y1 - 1];
	}
	if (!y1)
	{
		return sum[x2][y2] - sum[x1 - 1][y2];
	}
	return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
}
int main()
{
	pre_sum();
	cout << get_sum(1, 1, 2, 2) << endl;
	return 0;
}